package org.lsms.servlet;

/**
 * servlet invoked when author upload index information 
 */

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lsms.dao.impl.DaoImpl;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

public class paper_index_upload extends HttpServlet {

	public void destroy() {
		super.destroy(); 
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			
			int paper_id = Integer.parseInt(request.getParameter("paper_id"));
//			System.out.println(paper_id);
			
			SmartUpload myupload = new SmartUpload();
			myupload.initialize(this.getServletConfig(), request, response);
			try{
				myupload.upload();
			}catch(SmartUploadException e){
				e.printStackTrace();
			}
			
			//upload final paper
			
			final String FileType = "pdf,doc";
			final int MaxSize = 1024*1024*50;
			
			com.jspsmart.upload.File file1 = myupload.getFiles().getFile(0);
			if(!file1.isMissing()){
				String ext = file1.getFileExt().toLowerCase();
				if(FileType.indexOf(ext)==-1){
					String info = "Final paper format mismatch!Only .doc or .pdf files accept.";
					response.sendRedirect("author/author_show_info.jsp?author_info="+info);//wrong format
					return;
				}
				int filesize = file1.getSize();
				if(filesize>MaxSize){
					String info = "Final paper too large size!Paper must be less than 50MB.";
					response.sendRedirect("author/author_show_info.jsp?author_info="+info);//wrong size
					return;
				}	
			}else{
				String info = "Please select your final paper to submit!";
				response.sendRedirect("author/author_show_info.jsp?author_info="+info);//no content
				return;
			}
			
			String ext = file1.getFileExt().toLowerCase();
			String savename = "/final_paper/";
			DaoImpl impl = new DaoImpl();
			savename +=paper_id;
			savename +=".";
			savename +=ext;
			try{
				file1.saveAs(savename, file1.SAVEAS_VIRTUAL);
			}catch(SmartUploadException e){
				e.printStackTrace();
			}
			
			if(impl.materail_upload(paper_id, 1, savename)){
				
			}else{
				String info = "Problem occurs when upload final paper!Try again later.";
				response.sendRedirect("author/author_show_info.jsp?author_info="+info);
				return;
			}
			
			//upload registration form
			
			com.jspsmart.upload.File file2 = myupload.getFiles().getFile(1);
			if(!file2.isMissing()){
				String ext2 = file2.getFileExt().toLowerCase();
				if(FileType.indexOf(ext2)==-1){
					String info = "Registration form format mismatch!Only .doc or .pdf files accept.";
					response.sendRedirect("author/author_show_info.jsp?author_info="+info);//wrong format
					return;
				}
				int filesize = file2.getSize();
				if(filesize>MaxSize){
					String info = "Registration form too large size!Paper must be less than 50MB.";
					response.sendRedirect("author/author_show_info.jsp?author_info="+info);//wrong size
					return;
				}	
			}else{
				String info = "Please select your registration form to submit!";
				response.sendRedirect("author/author_show_info.jsp?author_info="+info);//no content
				return;
			}
			
			String ext2 = file2.getFileExt().toLowerCase();
			String savename2 = "/paper_upload/registration/";
			savename2 +=paper_id;
			savename2 +=".";
			savename2 +=ext2;
			try{
				file2.saveAs(savename2, file2.SAVEAS_VIRTUAL);
			}catch(SmartUploadException e){
				e.printStackTrace();
			}
			
			if(impl.materail_upload(paper_id, 2, savename2)){
				
			}else{
				String info = "Problem occurs when upload registration form!Try again later.";
				response.sendRedirect("author/author_show_info.jsp?author_info="+info);
				return;
			}
			
			//upload copyright form
			
			com.jspsmart.upload.File file3 = myupload.getFiles().getFile(2);
			if(!file3.isMissing()){
				String ext3 = file3.getFileExt().toLowerCase();
				if(FileType.indexOf(ext3)==-1){
					String info = "Copyright form format mismatch!Only .doc or .pdf files accept.";
					response.sendRedirect("author/author_show_info.jsp?author_info="+info);//wrong format
					return;
				}
				int filesize = file3.getSize();
				if(filesize>MaxSize){
					String info = "Copyright form too large size!Paper must be less than 50MB.";
					response.sendRedirect("author/author_show_info.jsp?author_info="+info);//wrong size
					return;
				}	
			}else{
				String info = "Please select your copyright form to submit!";
				response.sendRedirect("author/author_show_info.jsp?author_info="+info);//no content
				return;
			}
			
			String ext3 = file3.getFileExt().toLowerCase();
			String savename3 = "/paper_upload/copyright/";

			savename3 +=paper_id;
			savename3 +=".";
			savename3 +=ext3;
			try{
				file3.saveAs(savename3, file3.SAVEAS_VIRTUAL);
			}catch(SmartUploadException e){
				e.printStackTrace();
			}
			
			if(impl.materail_upload(paper_id, 3, savename3)){
				
			}else{
				String info = "Problem occurs when upload copyright form!Try again later.";
				response.sendRedirect("author/author_show_info.jsp?author_info="+info);
				return;
			}
			
			//upload bank transfer copy
			
			final String pic_type = "gif,jpg,png";
			com.jspsmart.upload.File file4 = myupload.getFiles().getFile(3);
			if(!file4.isMissing()){
				String ext4 = file4.getFileExt().toLowerCase();
				if(pic_type.indexOf(ext4)==-1){
					String info = "Bank transfer copy format mismatch!Only .gif , .jpg and .png files accept.";
					response.sendRedirect("author/author_show_info.jsp?author_info="+info);//wrong format
					return;
				}
				int filesize = file4.getSize();
				if(filesize>MaxSize){
					String info = "Picture too large size!Picture must be less than 50MB.";
					response.sendRedirect("author/author_show_info.jsp?author_info="+info);//wrong size
					return;
				}	
			}else{
				String info = "Please select your bank transfer copy to submit!";
				response.sendRedirect("author/author_show_info.jsp?author_info="+info);//no content
				return;
			}
			
			String ext4 = file4.getFileExt().toLowerCase();
			String savename4 = "/paper_upload/bank_transfer/";

			savename4 +=paper_id;
			savename4 +=".";
			savename4 +=ext4;
			try{
				file4.saveAs(savename4, file4.SAVEAS_VIRTUAL);
			}catch(SmartUploadException e){
				e.printStackTrace();
			}
			
			if(impl.materail_upload(paper_id, 4, savename4)){
				
			}else{
				String info = "Problem occurs when upload final paper!Try again later.";
				response.sendRedirect("author/author_show_info.jsp?author_info="+info);
				return;
			}
			
			String info = "Upload succeed!";
			response.sendRedirect("author/author_show_info.jsp?author_info="+info);
			
	}

	public void init() throws ServletException {
		// Put your code here
	}

}
